'use strict'

const Database = use('Database')
const log = use('Logger')
const Request = require('../Lib/Request')
const Util = require('../Lib/Util')
const BaseService = require('../BaseClass/BaseService')

class PriceGeneratorService extends BaseService {
  async initfun(ctx) {
    let random_id = ctx.body.random_id

    let sess_key = 'price_generator_' + random_id

    //ctx.session.put('user', 'wu')
    //ctx.session.get('user')

    let sess_val = [] //初始化数组，这里用来装答题的结果

    ctx.session.put(sess_key, JSON.stringify(sess_val)) //初始化sess_key，如果以前存在，也会被清空

    //ctx.session.put('user', JSON.stringify([1,2])
    //let arr = JSON.parse(ctx.session.get('user'))

    //获取答题列表给前台
    const mcPricingIndexTable = new (require('../Models/Table/mc_pricing_index'))('mc_pricing_index')

    let result = {}

    result = await mcPricingIndexTable.getResults()
    if (result.error) {
      throw new Error(result.msg)
    }

    //构建题目
    let data = []
    for (let item of result.data) {
      if (!item.parent_code) {
        data.push({ code: item.code, desc: item.desc, inputType: item.input_type, anwsers: [] })
      }
    }

    for (let item of result.data) {
      for (let d of data) {
        if (d.code === item.parent_code) d.anwsers.push({ code: item.code, desc: item.desc })
      }
    }

    return Util.end({ data: data })
  }

  async update(ctx) {
    try {
      let random_id = ctx.body.random_id
      let sess_key = 'price_generator_' + random_id
      let code = JSON.parse(ctx.session.get(sess_key))

      let data = { code: code }
      const mcPricingIndexTable = new (require('../Models/Table/mc_pricing_index'))('mc_pricing_index')
      let result = await mcPricingIndexTable.getPrices(data)

      let fb = {}

      if (result.data && result.msg == 'OK') {
        let baseScore = result.data[0].sm
        let difficulty = result.data[0].dif

        let arr = { baseScore: baseScore }

        const mcPricingResultTable = new (require('../Models/Table/mc_pricing_result'))('mc_pricing_result')
        let result2 = await mcPricingResultTable.getPrice(arr)
        let basePrice = -1
        let finalPrice = -1
        let maitainPrice = -1
        let maintainRate = 0.1 //维护费的比率

        if (code.includes('Q4-1') || code.includes('Q6-1') || code.includes('Q10-3')) {
          maintainRate = 0.3
        } else if (code.includes('Q4-2') || code.includes('Q10-2')) {
          maintainRate = 0.2
        }

        if (result2.msg == 'OK' && result2.data) {
          if (result2.data[0].price) {
            basePrice = result2.data[0].price
          }
        }
        log.info(basePrice)
        if (basePrice != -1) {
          finalPrice = basePrice * difficulty
          maitainPrice = finalPrice * maintainRate
        }

        fb.finalPrice = Util.mathRound(finalPrice, 2)
        fb.maitainPrice = Util.mathRound(maitainPrice, 2)
      }

      return Util.end({ data: fb })
    } catch (err) {
      return Util.error({
        msg: err.message,
        track: 'beiyao1',
      })
    }
  }
}

module.exports = PriceGeneratorService
